package com.is.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.is.control.UserControl;
import com.is.model.User;

public class LoginCheckFilter implements Filter{
    public void init(FilterConfig filterConfig)throws ServletException{}
    public void destroy(){}
    public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)
        throws IOException,ServletException
    {
        HttpServletRequest req=(HttpServletRequest)request;
        HttpServletResponse res=(HttpServletResponse)response;
        response.setContentType("text/html;charset=UTF-8"); 
        request.setCharacterEncoding("UTF-8");//设置字符编码
        response.setCharacterEncoding("UTF-8");
        res.setHeader("Pragma", "No-cache");//禁止缓存
        res.setHeader("Cache-Control","no-cache");
        res.setHeader("Expires", "0");
        PrintWriter out=res.getWriter();
        HttpSession session=req.getSession();
        Cookie cks[] = req.getCookies();
        String uid="";
        String pwd="";
        boolean isCookie=false;
        if(session.getAttribute("user")!=null){
       	   chain.doFilter(request, response);
          }
	    else{
	        if(cks!=null){
		        for(Cookie c:cks){
		             if(c.getName().equals("CK_UID")&&c.getValue()!=null&&!c.getValue().equals("")){
		                  uid =c.getValue();
		                  isCookie=true;
		             }
		             if(c.getName().equals("CK_PWD")&&c.getValue()!=null&&!c.getValue().equals("")){
		                pwd =c.getValue();
		            }
		        }
		        if(isCookie){
		            User cUser = UserControl.getUserById(Integer.parseInt(uid));
		            //System.out.println("SSS");
		            if(!pwd.equals("")&&cUser.getPassword().equals(pwd)){
		                req.getSession().setAttribute("user", cUser);
		                req.getSession().setAttribute("uid", cUser.getId());
		              //  System.out.println("验证COOKIE成功");
		                chain.doFilter(request, response);
		            }else{
		            	String pathtt = ((HttpServletRequest) request).getContextPath();
			            String basePathxbc = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pathtt+"/";
			        	String logout =basePathxbc+"login/login.jsp";
			        	//System.out.println(logout+"EEEE");
			            out.println(" <script  type='text/javascript'>alert('您上次登录可能修改了密码，请重新登录。'); </script>");
			            ((HttpServletResponse) response).setHeader("refresh","0;URL="+logout) ;
		            }
		           
		        }
		        }else{
			        	String pathtt = ((HttpServletRequest) request).getContextPath();
			            String basePathxbc = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+pathtt+"/";
			        	String logout =basePathxbc+"login/login.jsp";
			        	//System.out.println(logout+"EEEE");
			               out.println(" <script  type='text/javascript'>alert('您尚未登陆，若要继续浏览，请先登陆。'); </script>");
			               ((HttpServletResponse) response).setHeader("refresh","0;URL="+logout) ;
			        }
	           }
    }
}